package com.supermap.business.cloud.warninfo;

import com.alibaba.dubbo.config.annotation.Reference;
import com.supermap.business.cloud.push.MyWebSocket;
import com.supermap.business.push.getui.AppPush;
import com.supermap.dataservice.redis.JedisComponent;
import com.supermap.dataservice.util.JSONUtil;
import com.supermap.dubbo.api.cloud.push.Notice;
import com.supermap.dubbo.api.cloud.push.PushService;
import com.supermap.dubbo.api.cloud.warnInfo.WarnInfo;
import com.supermap.dubbo.api.cloud.warnInfo.WarningInfoService;
import com.supermap.dubbo.api.disaster.DisasterTypeService;
import com.supermap.dubbo.api.warn.FileShare;
import com.supermap.dubbo.api.warn.WarnCap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author aqua
 * @description
 * @date 2018年12月03 下午 5:25
 * @email 1301584033@qq.com
 */
@Controller
@RequestMapping("/cloud/warninfo")
public class WarningInfoController {
    @Autowired
    JedisComponent jedisComponent;
    @Reference(timeout = 10000)
    private WarningInfoService warningInfoService;
    @Reference(timeout = 50000)
    private DisasterTypeService disasterTypeService;
    @Reference(timeout = 100000)
    private PushService pushService;
    /**
     * 多条预警信息入库加推送
     * */

   //@Scheduled(fixedRate = 1000*60*60)
    public  void insertWarnInfoList(){
       String os = System.getProperty("os.name");
       System.out.println("推送数据"+"当前操作系统:"+os+"----------------------------------------------");
//       if(os.toLowerCase().indexOf("windows")!=-1){
//           System.out.println("判断为操作开发环境不推送预警信息，已退出！");
//            return;
//       }

        //初始化Json封装工具类
                    //重庆的区域
        // List<Map<String,Object>> mapList = disasterTypeService.getAllWarnArea("50");
        //获取所有区域
         List<Map<String,Object>> mapList = disasterTypeService.getAllArea();

         for(Map<String,Object> map:mapList){
             String areaCode=(String)map.get("areaCode");
             String warnAllArea=(String)map.get("warnAreaCode");
             if(areaCode!=null&&warnAllArea!=null){
                 insertLists(areaCode,warnAllArea);
             }
         }

    }
    public void insertLists(String areaCode,String warnAllArea){
        String noticetype=pushService.getProductNoticeTypes("quanguoapp2019131xsadwqeq");
        List<Notice> noticeList=new ArrayList<>();
        List<WarnInfo> warnInfoList=new ArrayList<>();
        try{
            ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
            FileShare client = (FileShare)context.getBean("client");
            List<WarnCap> customParseFileList = client.listWarnCapByElement(warnAllArea, "RED,BLUE,YELLOW,YELLOW,UNKOWN", "A", 10*6);
            //获取最近10分钟数据
            String dateTime=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
            if(customParseFileList!=null&&customParseFileList.size()>0){
                for (WarnCap warnCap:customParseFileList){
                    String warnId=String.valueOf(new Date().getTime());
                    warnId=warnId.substring(0,9);
                    WarnInfo warnInfo =new WarnInfo();
                    warnInfo.setId(warnId);
                    warnInfo.setDescription(warnCap.getDescription());
                    warnInfo.setEffective(warnCap.getEffective().substring(0,19));
                    warnInfo.setEventType(warnCap.getEventType());
                    warnInfo.setEventTypeCN(warnCap.getEventTypeCN());
                    warnInfo.setExpires(warnCap.getExpires().substring(0,19));
                    warnInfo.setHeadline(warnCap.getHeadline());
                    warnInfo.setIdentifier(warnCap.getIdentifier());
                    warnInfo.setMsgType(warnCap.getMsgType());
                    warnInfo.setNote(warnCap.getNote());
                    warnInfo.setReferencesInfo(warnCap.getReferencesInfo());
                    warnInfo.setSender(warnCap.getSender());
                    warnInfo.setSeverity(warnCap.getSeverity());
                    warnInfo.setAreaCode(areaCode);
                    warnInfoList.add(warnInfo);
                    List<String> integerList= pushService.getAllUserIdByAreaCode(areaCode);
                    if(integerList!=null&&integerList.size()>0){
                        for(String userId:integerList){
                            Notice notice = new Notice();
                            notice.setUploadtime(dateTime);
                            notice.setNoticetype(7);
                            notice.setMessageid(Integer.valueOf(warnId));
                            notice.setNoticetitle(warnInfo.getHeadline());
                            notice.setReceiveuserid(userId);
                            notice.setSenduserid("332606");
                            noticeList.add(notice);
                        }
                    }

                }
            }
            if(warnInfoList!=null&&warnInfoList.size()>0){
                warningInfoService.insertWarnInfoList(warnInfoList);
//                if(noticeList!=null&&noticeList.size()>0) {
//                    pushService.putNoticeList(noticeList);
//                    //app总数推送
//                    for (Notice notice : noticeList) {
//                        MyWebSocket.sendInfo(pushService.getReceiveuserIdCount(String.valueOf(notice.getReceiveuserid()),noticetype).getJsonStr(), String.valueOf(notice.getReceiveuserid()));
//                        List<String> strings=pushService.getUserCode(notice.getReceiveuserid()+"");
//                        AppPush.UserCodePush(strings,"您收到一条预警信息",notice.getNoticetitle());
//                    }
//
//                }
            }

        }catch (Exception e){
            e.printStackTrace();

        }
    }

    /**
     *获取预警详细信息
     * */
    @GetMapping("/getWarnInfoById")
    @ResponseBody
    public JSONUtil getWarnInfoById(Integer warnInfoId) {
        return warningInfoService.getWarnInfoById(warnInfoId);
    }

    /**
     *获取预警信息列表
     * */
    @GetMapping("/getPageWarnInfoByTimeOrAreaCode")
    @ResponseBody
    public JSONUtil getPageWarnInfoByTimeOrAreaCode(String areaCode,String aging,String beginTime,String endTime,String eType,Integer pageNum,Integer pageSize) {
        String key="/cloud/warninfo/getPageWarnInfoByTimeOrAreaCode"+areaCode+aging+beginTime+endTime+eType+pageNum+pageSize;
        JSONUtil jsonUtils=null;
        if(jedisComponent.exists(key)){
            jsonUtils=new JSONUtil().setData(jedisComponent.getObject(key));
            return  jsonUtils;
        }else{
            jsonUtils= warningInfoService.getPageWarnInfoByTimeOrareaCode(areaCode,aging, beginTime,endTime,eType,pageNum,pageSize);
        }
        //设置缓存
        jedisComponent.setObject(key,jsonUtils.getData());
        //设置时效 保存半个小时
        jedisComponent.setKeyTime(key,60);
        //设置时效 保存半个小时
        return jsonUtils;
    }

    /**
     *获取指定区域、时间范围内的预警类型
     * */
    @GetMapping("/getWarnTypeByTimeOrAreaCode")
    @ResponseBody
    public JSONUtil getWarnTypeByTimeOrAreaCode(String areaCode,String beginTime,String endTime) {
        String key="/cloud/warninfo/getWarnTypeByTimeOrAreaCode"+areaCode+beginTime+endTime;
        JSONUtil jsonUtils=null;
        if(jedisComponent.exists(key)){
            jsonUtils=new JSONUtil().setData(jedisComponent.getObject(key));
            return  jsonUtils;
        }else{
            jsonUtils= warningInfoService.getWarnTypeByTimeOrAreaCode(areaCode,beginTime,endTime);
        }
        //设置缓存
        jedisComponent.setObject(key,jsonUtils.getData());
        //设置时效 保存半个小时
        jedisComponent.setKeyTime(key,3600);
        //设置时效 保存半个小时
        return jsonUtils;

    }

}
